package org.example.now.dao;

import org.apache.ibatis.annotations.Param;
import org.example.now.entity.UserInfoDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.List;


/**
 * JPA接口
 */
@Component
public interface UserDao extends JpaRepository<UserInfoDto,Integer> {
    // 查询以某个字符开始的所有人
    List<UserInfoDto> getUserByUserCodeStartingWith(String userCode);

    /**
     * 查询id大于某个值的所有人
     * @param id
     * @return
     */
    List<UserInfoDto> getUserByIdGreaterThan(Integer id);

    @Query(value = "select * from user where id=(select max(id) from user)", nativeQuery = true)
    UserInfoDto getMaxIdBook();

    @Query(value = "select u from UserInfoDto u where  u.id>:id and u.userCode =:userCode")
    List<UserInfoDto> getUserByIdAndUserCode(@Param("userCode") String userCode,@Param("id") Integer id);

    @Query(value = "select id,user_code,user_name from user u where  u.id<?2 and u.user_code like %?1%", nativeQuery = true)
    List<UserInfoDto> getUserByIdAndName(String name,Integer id);


    @Transactional
    @Modifying
    @Query(value = "update user u set u.user_name=:userName where u.user_code=:userCode",nativeQuery = true)
    void  updateUserName(@Param("userCode") String userCode,@Param("userName") String userName);

    @Transactional
    @Modifying
    @Query(value = "update user u set u.user_name=?2 where u.user_code=?1",nativeQuery = true)
    void updateUserName2(@Param("userCode") String userCode,@Param("userName") String userName);


}
